home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / INDEX.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  6KB  |  178 lines

  1. /* +++Date last modified: 17-Nov-1996 */
  2.  
  3. /*
  4. **  INDEX.C
  5. **
  6. **  This is the indexing function for creating a binary file from an ASCII
  7. **  file formatted as follows:
  8. **
  9. **  Mark Corgan
  10. **  550 Foothill Rd.
  11. **  Gardnerville, NV 89410
  12. **  (702) 265-2388
  13. **  .
  14. **  Hello World
  15. **  123 Anywhere St.
  16. **  Anytown, CA 12345
  17. **  (123) 456-7890
  18. **  .
  19. **  etc...
  20. **  
  21. **  The period is what the companion LOOKUP.C looks for to indicate the end
  22. **  of record. Of course, you could have any format you like, so long as the
  23. **  first line is the information you are looking for. Also, there is no
  24. **  limit to the number of lines of infomation after the first line and
  25. **  before the period as fputs() continues until the period. Enjoy!
  26. **
  27. **  by Mark Corgan, 09-May-1993, and donated to the public domain
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "errors.h"                   /* for cant()                       */
  33. #include "indxlook.h"                 /* definitions for INDEX and LOOKUP */
  34.  
  35. struct tree_node                      /* node in tree */
  36. {
  37.    struct tree_node *l_ptr, *r_ptr;   /* left and right pointers */
  38.    INDEX *data_ptr;                   /* data pointer */
  39. };
  40.  
  41. typedef struct tree_node TREE_NODE;
  42.  
  43. void        write_index(char *txtfile, char *ndxfile);
  44. void        save_tree(TREE_NODE * root, FILE *fp);
  45. TREE_NODE  *make_tree(FILE *fp, long *cnt_ptr);
  46. TREE_NODE  *insert_tree(TREE_NODE *root, INDEX *rec_ptr, long *cnt_ptr);
  47. long        bsearch_(FILE *ifp, long first, long last, char *target);
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51.       if (argc != 3)
  52.       {
  53.             puts("Usage: INDEX text_file_name index_file_name\n");
  54.             puts("Note: The text file must consist of a number of records "
  55.                  "separated by lines");
  56.             puts("      containing a single period (\".\")");
  57.             return EXIT_FAILURE;
  58.       }
  59.       write_index(argv[1], argv[2]);
  60. }
  61.  
  62. void write_index(char *txtfile, char *ndxfile)
  63. {
  64.       FILE *afp, *ifp;                    /* types of files       */
  65.       TREE_NODE *root;                    /* index tree           */
  66.       static INDEX header = {"!", 0};     /* dummy header node    */
  67.  
  68.       afp = cant(txtfile, "r");
  69.       if ((root = make_tree(afp, &header.pos)) != NULL)
  70.       {
  71.             ifp = cant(ndxfile, "wb");
  72.             fwrite((char *) &header, sizeof(header), 1, ifp);
  73.             save_tree(root, ifp);
  74.             fclose(ifp);
  75.             printf("\n%ld records\n", header.pos);
  76.       }
  77.       fclose(afp);
  78. }
  79.  
  80. /*
  81. ** Make index tree
  82. */
  83.  
  84. TREE_NODE *make_tree(FILE *fp,            /* file                 */
  85.                      long *cnt_ptr)       /* count of records     */
  86. {
  87.       TREE_NODE *root = NULL, *temp_ptr;  /* add node to tree     */
  88.       char line[MAX_LINE];                /* next line            */
  89.       long start_pos = 0;
  90.       INDEX *next_ptr;                    /* next key, pos pair   */
  91.       /* starting with new record */
  92.       Boolean_T new_record = True_, have_mem = True_;
  93.  
  94.       *cnt_ptr = 0;
  95.       while (start_pos = ftell(fp), have_mem && fgets(line,sizeof(line), fp))
  96.       {
  97.             if (new_record)
  98.             {
  99.                   if ((next_ptr = (INDEX *) malloc(sizeof(INDEX))) != NULL)
  100.                   {
  101.                         strncpy(next_ptr->key, line, MAX_KEY);
  102.  
  103.                         next_ptr->pos = start_pos;
  104.                         temp_ptr      = insert_tree(root, next_ptr, cnt_ptr);
  105.  
  106.                         if (temp_ptr)
  107.                               root = temp_ptr;
  108.                   }
  109.                   have_mem = next_ptr && temp_ptr;
  110.             }
  111.             new_record = strcmp(line, END_REC) == 0;
  112.       }
  113.       if (!have_mem)
  114.             fprintf(stderr, "Out of memory. Key: %s\n", line);
  115.  
  116.       return root;
  117. }
  118.  
  119. /*
  120. ** Save the index tree to a file
  121. */
  122.  
  123. void save_tree(TREE_NODE *root, FILE *fp)
  124. {
  125.       if (root)
  126.       {
  127.             save_tree(root->l_ptr, fp);
  128.             fwrite(root->data_ptr, sizeof(INDEX), 1, fp);
  129.             save_tree(root->r_ptr, fp);
  130.       }
  131. }
  132.  
  133. /*
  134. ** Add record to tree
  135. */
  136.  
  137. TREE_NODE *insert_tree(TREE_NODE *root,         /* pointer to tree      */
  138.                        INDEX *rec_ptr,          /* record to install    */
  139.                        long *cnt_ptr)           /* nodes in tree        */
  140.  
  141. {
  142.       if (root)
  143.       {
  144.             int cmp = strncmp(rec_ptr->key, root->data_ptr->key, MAX_KEY);
  145.  
  146.             if (cmp < 0)                        /* left side?           */
  147.                   root->l_ptr = insert_tree(root->l_ptr, rec_ptr, cnt_ptr);
  148.             else if (cmp > 0)                   /* right side           */
  149.                   root->r_ptr = insert_tree(root->r_ptr, rec_ptr, cnt_ptr);
  150.             else  fprintf(stderr, "Duplicate key: %s\n", rec_ptr->key);
  151.       }
  152.       else if (root = (TREE_NODE *) malloc(sizeof(TREE_NODE)), root)
  153.       {
  154.             root->data_ptr = rec_ptr;
  155.             root->l_ptr = root->r_ptr = NULL;
  156.             (*cnt_ptr)++;
  157.       }
  158.       return root;
  159. }
  160.  
  161. long bsearch_(FILE *ifp, long first, long last, char *target)
  162. {
  163.       long pos, mid =(first + last) / 2;
  164.       INDEX next;
  165.       int cmp;
  166.  
  167.       if (mid < first || fseek(ifp, mid * sizeof(INDEX), 0) != 0 ||
  168.           fread((char *) &next, sizeof(INDEX), 1, ifp) == 0)
  169.       {
  170.             pos = -1;
  171.       }
  172.       else  pos = ((cmp = strncmp(target, next.key, MAX_KEY)) == 0) ?
  173.                   next.pos :
  174.                   ((cmp < 0) ? bsearch_(ifp, first, mid - 1, target)
  175.                              : bsearch_(ifp, mid + 1, last, target));
  176.       return pos;
  177. }
  178.